package com.zwb.service.vo;

import com.zwb.service.enums.StatusCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author 头发又黑又长
 * @Date 2022/8/26 20:08
 * @email zwb15083976291@163.com
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class R<T> implements Serializable {

    private Long code;
    private String message;
    private T data;

    public R(StatusCode statusCode) {
        this.code = statusCode.getCode();
        this.message = statusCode.getMessage();
        this.data = null;
    }

    /**
     * 成功返回
     *
     * @return
     */
    public static <T> R<T> success() {
        return new R(StatusCode.SUCCESS);
    }

    /**
     * 成功返回添加信息
     *
     * @param data
     * @return
     */
    public static <T> R<T> success(T data) {
        R<T> r = new R<T>(StatusCode.SUCCESS);
        r.setData(data);
        return r;
    }


    public static <T> R<T> success(String message, T data) {
        R<T> r = new R<T>(StatusCode.SUCCESS);
        r.setMessage(message);
        r.setData(data);
        return r;
    }

    /**
     * 成功返回添加 message
     *
     * @param message
     * @return
     */
    public static <T> R<T> success(String message) {
        R<T> r = new R<T>(StatusCode.SUCCESS);
        r.setMessage(message);
        return r;
    }


    /**
     * 失败
     *
     * @return
     */
    public static <T> R<T> error() {
        return new R<T>(StatusCode.FAIL);
    }

    /**
     * 失败添加而外信息
     *
     * @return
     */
    public static <T> R<T> error(String message) {
        R<T> r = new R<>(StatusCode.FAIL);
        r.setMessage(message);
        return r;
    }

    /**
     * 返回指定的 状态码
     *
     * @param statusCode
     * @param <T>
     * @return
     */
    public static <T> R<T> result(StatusCode statusCode) {
        R<T> r = new R<T>(statusCode);
        return r;
    }

    /**
     * 指定信息 和状态码
     *
     * @param message
     * @param statusCode
     * @return
     */
    public static <T> R<T> result(String message, StatusCode statusCode) {
        R<T> r = new R<T>(statusCode);
        r.setMessage(message);
        return r;
    }


    /**
     * 指定所有返回
     *
     * @param message
     * @param data
     * @param statusCode
     * @return
     */
    public static <T> R<T> result(String message, T data, StatusCode statusCode) {
        R<T> r = new R<T>(statusCode);
        r.setMessage(message);
        r.setData(data);
        return r;
    }


}
